use idgenerator::{IdGeneratorOptions, IdInstance};
use sea_orm::sqlx::types::chrono::NaiveDate;
use std::sync::OnceLock;
use uuid::Uuid;

const ID_INIT: OnceLock<()> = OnceLock::new();

/// 通过雪花算法生成id
pub fn next_id() -> i64 {
    ID_INIT.get_or_init(|| {
        let timestamp = NaiveDate::from_ymd_opt(2025, 6, 1)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap()
            .and_utc()
            .timestamp_millis();
        let options = IdGeneratorOptions::new()
            .base_time(timestamp)
            .worker_id(1)
            .worker_id_bit_len(4);
        // 初始化实例
        IdInstance::init(options).unwrap();
    });

    IdInstance::next_id()
}

/// 生成uuid
pub fn uuid_simple() -> String {
    let id = Uuid::new_v4().simple();
    id.to_string()
}

/// 生成uuid
pub fn uuid() -> String {
    let id = Uuid::new_v4();
    id.to_string()
}

/// 生成xid
pub fn xid() -> String {
    let id = xid::new();
    id.to_string()
}

